﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace 案例129
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public byte[] GetFileBytes(string Filename)
        {
            if (Filename == "")
                return null;
            try
            {

                FileStream fileStream = new FileStream(Filename, FileMode.Open, FileAccess.Read);
                BinaryReader binaryReader = new BinaryReader(fileStream);
                byte[] fileBytes = binaryReader.ReadBytes((int)fileStream.Length);
                binaryReader.Close();
                fileStream.Close();
                return fileBytes;
            }
            catch
            {
                return null;
            }
        }

        public bool WriteFileBytes(string TargetFilename, byte[] fileBytes)
        {
            bool k = true;
            if (TargetFilename != "" && fileBytes.Length != 0)
            {
                try
                {
                    FileStream fileStream = new FileStream(TargetFilename, FileMode.OpenOrCreate,
                        FileAccess.Write);
                    BinaryWriter binaryWriter = new BinaryWriter(fileStream);
                    binaryWriter.Write(fileBytes);
                    binaryWriter.Flush();
                    binaryWriter.Close();
                    fileStream.Close();
                }
                catch
                {
                    k = false;
                }
            }
            else
            {
                k = false;
            }
            return k;
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openfile = new OpenFileDialog();
                openfile.Filter = "jpg类型图片(*.jpg)|*.jpg|BMP类型图片(*.bmp)|*.bmp";
                if (openfile.ShowDialog() == DialogResult.OK)
                {
                    byte[] picbinary = GetFileBytes(openfile.FileName);
                    MemoryStream mempicstream = new MemoryStream(picbinary);
                    mempicstream.Dispose();
                    mempicstream.Close();
                }
            }
            catch (Exception m)
            {
                MessageBox.Show("读取图片出错，可能的问题是：" + Convert.ToString(m), "错误提示");
            }
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            try
            {
                if (pictureBox1.Image == null)
                {
                    MessageBox.Show("禁止图片为空时候另存信息。", "错误提示");
                }
                else
                {
                    saveFileDialog1.Filter = "jpg类型图片(*.jpg)|*.jpg";
                    DialogResult result = saveFileDialog1.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        MemoryStream ms = new MemoryStream();
                        Bitmap bm = new Bitmap(pictureBox1.Image);
                        bm.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                        byte[] bytes = ms.ToArray();
                        WriteFileBytes(saveFileDialog1.FileName, bytes);
                        MessageBox.Show("另存图片成功", "提示");
                        ms.Dispose();
                        ms.Close();
                        bm.Dispose();
                    }
                }
            }
            catch (Exception m)
            {
                MessageBox.Show("读取图片出错，可能的问题是：" + Convert.ToString(m), "错误提示");

            }

        }
    }
}
